package dao;

import java.util.List;

import bean.User;

import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import bean.User;
import utils.DBCPUtil;

public class UserManagerDaoImpl implements UserManagerDao {
	QueryRunner qr = new QueryRunner(DBCPUtil.getDataSource());
	/**
	 * 查询用户名为name的用户是否存在
	 * @param name
	 * @return
	 */
	@Override
	public boolean isUser(String user_id) {
		// TODO Auto-generated method stub
		User u = null;
		try {
			u = qr.query("select * from user where user_id=?", new BeanHandler<>(User.class), user_id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (u != null) return true;
		return false;
	}
	
	@Override
	public List<User> getallUser(){
		List<User> users=new ArrayList<User>();
		try {
			//如果有多组数据，结果对应到ArrayList, BeanHandler改为BeanListHandler,注释部分为样例       这里我改了一下
			//List<User> list = new ArrayList<User>();
			//list = qr.query("select * from user where name=?", new BeanListHandler<>(User.class), name);
			users = qr.query("select * from user", new BeanListHandler<>(User.class));
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return users;
	}
	/**
	 * 通过用户名获取到对应user的数据，如果没有，返回null
	 * @param name
	 * @param passwd
	 * @return
	 */
	@Override
	public List<User> getUser(String id) {
		// TODO Auto-generated method stub
		List<User> users=new ArrayList<User>();
		try {
			//如果有多组数据，结果对应到ArrayList, BeanHandler改为BeanListHandler,注释部分为样例       这里我改了一下
			//List<User> list = new ArrayList<User>();
			//list = qr.query("select * from user where name=?", new BeanListHandler<>(User.class), name);
			users = qr.query("select * from user where user_id=?", new BeanListHandler<>(User.class),id);
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return users;
	}
	/*@Override
	public List<User> getUserByName(String name) {
		// TODO Auto-generated method stub
		List<User> users=new ArrayList<User>();
		try {
			//如果有多组数据，结果对应到ArrayList, BeanHandler改为BeanListHandler,注释部分为样例       这里我改了一下
			//List<User> list = new ArrayList<User>();
			//list = qr.query("select * from user where name=?", new BeanListHandler<>(User.class), name);
			users = qr.query("select * from user where name=?", new BeanListHandler<>(User.class),name);
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return users;
	}*/
	@Override
	public List<User> getUserByAdmin(int admin) {
		// TODO Auto-generated method stub
		List<User> users=new ArrayList<User>();
		try {
			//如果有多组数据，结果对应到ArrayList, BeanHandler改为BeanListHandler,注释部分为样例       这里我改了一下
			//List<User> list = new ArrayList<User>();
			//list = qr.query("select * from user where name=?", new BeanListHandler<>(User.class), name);
			users = qr.query("select * from user where admin=?", new BeanListHandler<>(User.class),admin);
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return users;
	}

	/**
	 * 得到数据库中第st行到en行的用户数据，用于用户管理页面的分页排版
	 * @param st
	 * @param en
	 * @return
	 */
	@Override
	public List<User> getPageUser(int st, int en) {
		// TODO Auto-generated method stub
		List<User> users=new ArrayList<User>();
		try {
			users=qr.query("select * from user limit ?,?",new BeanListHandler<>(User.class),st-1,en-st+1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return users;
	}
	/**
	 * 添加用户数据到数据库，用于注册等场景，注册成功返回1，用户名已存在返回0，其它情况返回-1
	 * @param user
	 * @return
	 */
	@Override
	public int addUser(User user) {
		// TODO Auto-generated method stub
		User u = null;
		try {
			u = qr.query("select * from user where user_id=?", new BeanHandler<>(User.class), user.getUser_id());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (u != null) return 0;        //用户已存在
		try {
			qr.update("insert into user(user_id, password, author,admin) values(?,?,?,?)", user.getUser_id(), user.getPassword(), user.getAuthor(),user.isAdmin());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}
		return 1;
	}
	/**
	 * 用于修改用户的信息，例如密码等
	 * @param user
	 * @return
	 */
	@Override
	public boolean updateUser(User user,String password,int admin) {
		// TODO Auto-generated method stub
		try {
			//如果有多组数据，结果对应到ArrayList, BeanHandler改为BeanListHandler,注释部分为样例       这里我改了一下
			//List<User> list = new ArrayList<User>();
			//list = qr.query("select * from user where name=?", new BeanListHandler<>(User.class), name);
			qr.update("update user set password=?, admin=? where user_id=?", password,admin,user.getUser_id());
		}catch(SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	/**
	 * 删除用户用户名为name的用户，慎用
	 * @param name
	 * @return
	 */
	@Override
	public boolean deleteUser(String id) {
		// TODO Auto-generated method stub
		try {
			qr.update("delete from mail_of_user where user_id=?",id);
			qr.update("delete from user where user_id=?", id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public boolean pwdIsTrue(String pwd, String user_id) {
		// TODO Auto-generated method stub
		User u = null;
		try {
			u = qr.query("select * from user where user_id=?", new BeanHandler<>(User.class), user_id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(u==null){
			return false;
		}
	    if (u.getPassword().equals(pwd)) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean admIsTrue(String user_id) {
		// TODO Auto-generated method stub
		User admin=null;
		try {
			admin = qr.query("select * from user where user_id=? and admin=1", new BeanHandler<>(User.class), user_id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(admin==null){
			return false;
		}
		/*if (admin==1) {
			return true;
		} */else {
			return true;
		}
	}
	
	/**
	 * 查询用户名为name的邮件数量，邮件总大小，修改时间2018/4/24 13:55
	 * @param name
	 * @return
	 */
	@Override
	public int getUserTotalMail(String user_id) {
		List<User> users=new ArrayList<User>();
		try {
			//如果有多组数据，结果对应到ArrayList, BeanHandler改为BeanListHandler,注释部分为样例       这里我改了一下
			users = qr.query("select * from mail_of_user where user_id = ?", new BeanListHandler<>(User.class), user_id);
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return users.size();
	}

	
}
